package com.czl.base.data

import com.czl.base.base.BaseBean
import com.czl.base.base.BaseModel
import com.czl.base.data.bean.*
import com.czl.base.data.bean.tengyun.*
import com.czl.base.data.bean.tengyun.NoticeListBean
import com.czl.base.data.db.SearchHistoryEntity
import com.czl.base.data.db.WebHistoryEntity
import com.czl.base.data.source.HttpDataSource
import com.czl.base.data.source.LocalDataSource
import com.czl.base.data.source.tengyun.TengyunDateSource
import io.reactivex.Flowable
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import java.io.Serializable

/**
 * @author Alwyn
 * @Date 2020/7/20
 * @Description 数据中心（本地+在线） 外部通过Koin依赖注入调用
 * 对于缓存或者在线数据的增删查改统一通过该数据仓库调用
 */
class DataRepository constructor(
    private val mLocalDataSource: LocalDataSource,
    private val mHttpDataSource: HttpDataSource,
    private val mTengyunDateSource: TengyunDateSource
) : BaseModel(), LocalDataSource, HttpDataSource, TengyunDateSource {

    //    companion object {
//        @Volatile
//        private var INSTANCE: DataRepository? = null
//        fun getInstance(
//            localDataSource: LocalDataSource,
//            httpDataSource: HttpDataSource
//        ): DataRepository? {
//            if (INSTANCE == null) {
//                synchronized(DataRepository::class.java) {
//                    if (INSTANCE == null) {
//                        INSTANCE = DataRepository(localDataSource, httpDataSource)
//                    }
//                }
//            }
//            return INSTANCE
//        }
//    }


    override fun logout(): Observable<BaseBean<Any?>> {
        return mHttpDataSource.logout()
    }

    override fun getHotLine(areaId: String): Observable<BaseBean<String>> {
        return mHttpDataSource.getHotLine(areaId)
    }

    override fun getDeleteNotice(): Observable<BaseBean<String>> {
        return mHttpDataSource.getDeleteNotice()
    }

    override fun verifyPwdNet(
        newPassword: String,
        oldPassword: String
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.verifyPwdNet(newPassword, oldPassword)
    }

    override fun initPassword(
        newPassword: String
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.initPassword(newPassword)
    }

    override fun deleteUserAccount(): Observable<BaseBean<Any?>> {
        return mHttpDataSource.deleteUserAccount()
    }

    override fun cancelWorkOrder(no: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.cancelWorkOrder(no)
    }

    override fun readNotice(msgId: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.readNotice(msgId)
    }

    override fun deleteWorkOrder(no: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.deleteWorkOrder(no)
    }

    override fun cancelComplaint(no: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.cancelComplaint(no)
    }

    override fun deleteComplaint(no: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.deleteComplaint(no)
    }

    override fun addPersonCar(params: Map<String, String>): Observable<BaseBean<Any?>> {
        return mHttpDataSource.addPersonCar(params)
    }

    override fun getUserRooms(phone: String, areaId: String): Observable<BaseBean<List<RoomBean>>> {
        return mHttpDataSource.getUserRooms(phone, areaId)
    }

    override fun getApplyDetail(id: String): Observable<BaseBean<UserDetailBean>> {
        return mHttpDataSource.getApplyDetail(id)
    }

    override fun getOwnerDetail(id: String): Observable<BaseBean<UserDetailBean>> {
        return mHttpDataSource.getOwnerDetail(id)
    }

    override fun getReverseSearchCar(reverseSearchBean: ReverseCarRequestBean): Observable<BaseBean<ReverseSearchBean>> {
        return mHttpDataSource.getReverseSearchCar(reverseSearchBean)
    }

    override fun register(
        username: String,
        password: String,
        repassword: String
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.register(username, password, repassword)
    }

    override fun getRecordDetail(orderNo: String): Observable<BaseBean<RecordDetailBean>> {
        return mHttpDataSource.getRecordDetail(orderNo)
    }

    override fun queryPayResult(
        orderNo: String,
        areaId: String
    ): Observable<BaseBean<PayResultBean>> {
        return mHttpDataSource.queryPayResult(orderNo, areaId)
    }

    override fun getMonthCardList(
        pageNum: Int,
        pageSize: Int,
        areaId: String
    ): Observable<BaseBean<MonthCardListBean>> {
        return mHttpDataSource.getMonthCardList(pageNum, pageSize, areaId)
    }

    override fun getRepairList(
        params: Map<String, String>
    ): Observable<BaseBean<ReportListBean>> {
        return mHttpDataSource.getRepairList(params)
    }

    override fun getNoticeMsgList(
        params: Map<String, String>
    ): Observable<BaseBean<com.czl.base.data.bean.NoticeListBean>> {
        return mHttpDataSource.getNoticeMsgList(params)
    }

    override fun getComplaintList(params: Map<String, String>): Observable<BaseBean<ReportListBean>> {
        return mHttpDataSource.getComplaintList(params)
    }

    override fun getScoreList(
        params: Map<String, String>
    ): Observable<BaseBean<ScoreListBean>> {
        return mHttpDataSource.getScoreList(params)
    }

    override fun addPeople(
        params: Map<String, String>
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.addPeople(params)
    }

    override fun updatePeople(
        params: Map<String, String>
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.updatePeople(params)
    }

    override fun getApplyRecords(
        areaId: String
    ): Observable<BaseBean<List<AddRecordBean>>> {
        return mHttpDataSource.getApplyRecords(areaId)
    }

    override fun deleteApply(
        id: String
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.deleteApply(id)
    }

    override fun deleteHouseUser(
        id: String
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.deleteHouseUser(id)
    }

    override fun getOwnerRecords(
        areaId: String,
        houseId: String
    ): Observable<BaseBean<List<PeopleManagerBean>>> {
        return mHttpDataSource.getOwnerRecords(areaId, houseId)
    }

    override fun getPointDetail(
        areaNo: String
    ): Observable<BaseBean<String>> {
        return mHttpDataSource.getPointDetail(areaNo)
    }

    override fun getIntegralList(
        params: Map<String, String>
    ): Observable<BaseBean<IntegralBean>> {
        return mHttpDataSource.getIntegralList(params)
    }

    override fun getPayRecordList(
        params: Map<String, String>
    ): Observable<BaseBean<PayRecordListBean>> {
        return mHttpDataSource.getPayRecordList(params)
    }

    override fun placeAnOrder(
        params: Map<String, String>
    ): Observable<BaseBean<PayInfoBean>> {
        return mHttpDataSource.placeAnOrder(params)
    }

    override fun payWorkOrder(
        params: Map<String, String>
    ): Observable<BaseBean<WorkOrderPayBean>> {
        return mHttpDataSource.payWorkOrder(params)
    }

    override fun getMyCarList(
        vehiclesWithNoPlan: Boolean,
        areaId: String
    ): Observable<BaseBean<List<CarItem>>> {
        return mHttpDataSource.getMyCarList(vehiclesWithNoPlan, areaId)
    }

    override fun deleteCarList(
        vehicleNos: List<String>,
        areaId: String
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.deleteCarList(vehicleNos, areaId)
    }

    override fun deleteQueryCar(vehicleNo: String, areaId: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.deleteQueryCar(vehicleNo, areaId)
    }

    override fun getMyQueryHistory(): Observable<BaseBean<List<CarItem>>> {
        return mHttpDataSource.getMyQueryHistory()
    }

    override fun getVersionName(): Observable<BaseBean<Any?>> {
        return mHttpDataSource.getVersionName()
    }


    override fun getLocalData(): String {
        return mLocalDataSource.getLocalData()
    }

    override fun getLoginName(): String? {
        return mLocalDataSource.getLoginName()
    }

    override fun getLoginPhone(): String? {
        return mLocalDataSource.getLoginPhone()
    }

    override fun getSipServerUrl(): String? {
        return mLocalDataSource.getSipServerUrl()
    }

    override fun getLoginToken(): String? {
        return mLocalDataSource.getLoginToken()
    }

    override fun saveUserData(userBean: UserBean) {
        mLocalDataSource.saveUserData(userBean)
    }

    override fun getLoginPwd(): String? {
        return mLocalDataSource.getLoginPwd()
    }

    override fun saveLoginPwd(pwd: String) {
        mLocalDataSource.saveLoginPwd(pwd)
    }

    override fun saveIsRememberPwd(isRemember: Boolean) {
        mLocalDataSource.saveIsRememberPwd(isRemember)
    }

    override fun saveRoomIdAndCode(idAndCode: String) {
        mLocalDataSource.saveRoomIdAndCode(idAndCode)
    }

    override fun isRememberPwd(): Boolean {
        return mLocalDataSource.isRememberPwd()
    }

    override fun getRoomIdAndCode(): String? {
        return mLocalDataSource.getRoomIdAndCode()
    }

    override fun saveUserInfoData(userInfo: UserInfo) {
        mLocalDataSource.saveUserInfoData(userInfo)
    }

    override fun getUserData(): UserBean? {
        return mLocalDataSource.getUserData()
    }

    override fun getUserId(): Int {
        return mLocalDataSource.getUserId()
    }

    override fun clearLoginState() {
        mLocalDataSource.clearLoginState()
    }

    override fun clearAllData() {
        mLocalDataSource.clearAllData()
    }

    override fun saveUserSearchHistory(keyword: String): Flowable<Boolean> {
        return mLocalDataSource.saveUserSearchHistory(keyword)
    }

    override fun getSearchHistoryByUid(): Flowable<List<SearchHistoryEntity>> {
        return mLocalDataSource.getSearchHistoryByUid()
    }

    override fun deleteSearchHistory(history: String): Disposable {
        return mLocalDataSource.deleteSearchHistory(history)
    }

    override fun deleteAllSearchHistory(): Observable<Int> {
        return mLocalDataSource.deleteAllSearchHistory()
    }

    override fun saveUserBrowseHistory(title: String, link: String) {
        return mLocalDataSource.saveUserBrowseHistory(title, link)
    }

    override fun getUserBrowseHistoryByUid(): Flowable<List<WebHistoryEntity>> {
        return mLocalDataSource.getUserBrowseHistoryByUid()
    }

    override fun deleteBrowseHistory(title: String, link: String): Observable<Int> {
        return mLocalDataSource.deleteBrowseHistory(title, link)
    }

    override fun deleteAllWebHistory(): Observable<Int> {
        return mLocalDataSource.deleteAllWebHistory()
    }

    override fun saveFollowSysModeFlag(isFollow: Boolean) {
        return mLocalDataSource.saveFollowSysModeFlag(isFollow)
    }

    override fun getFollowSysUiModeFlag(): Boolean {
        return mLocalDataSource.getFollowSysUiModeFlag()
    }

    override fun saveUiMode(nightModeFlag: Boolean) {
        return mLocalDataSource.saveUiMode(nightModeFlag)
    }

    override fun getUiMode(): Boolean {
        return mLocalDataSource.getUiMode()
    }

    override fun saveReadHistoryState(visible: Boolean) {
        return mLocalDataSource.saveReadHistoryState(visible)
    }

    override fun getReadHistoryState(): Boolean {
        return mLocalDataSource.getReadHistoryState()
    }

    override fun savePrivacyState(visible: Boolean) {
        return mLocalDataSource.savePrivacyState(visible)
    }

    override fun getPrivacyState(): Boolean {
        return mLocalDataSource.getPrivacyState()
    }

    override fun getAreaName(): String {
        return mLocalDataSource.getAreaName()
    }

    override fun saveAreaName(areaName: String) {
        return mLocalDataSource.saveAreaName(areaName)
    }

    override fun getAreaId(): String {
        return mLocalDataSource.getAreaId()
    }

    override fun saveAreaId(areaId: String) {
        return mLocalDataSource.saveAreaId(areaId)
    }

    override fun getAreaList(): List<UserBean.Data> {
        return mLocalDataSource.getAreaList()
    }

    override fun getLoginOutFlag(): Int {
        return mLocalDataSource.getLoginOutFlag()
    }

    override fun saveLoginOutFlag(loginOutFlag: Int) {
        return mLocalDataSource.saveLoginOutFlag(loginOutFlag)
    }

    override fun saveLoginToken(token: String) {
        return mLocalDataSource.saveLoginToken(token)
    }

    override fun <T : Serializable> saveCacheListData(list: List<T>) {
        return mLocalDataSource.saveCacheListData(list)
    }

    override fun <T : Serializable> getCacheListData(key: String): List<T>? {
        return mLocalDataSource.getCacheListData(key)
    }

    override fun loginByPwd(paramsJson: String): Observable<BaseBean<UserBean>> {
        return mHttpDataSource.loginByPwd(paramsJson)
    }

    override fun loginByPhoneCode(paramsJson: String): Observable<BaseBean<UserBean>> {
        return mHttpDataSource.loginByPhoneCode(paramsJson)
    }

    override fun getPhoneCode(phone: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.getPhoneCode(phone)
    }

    override fun evaluateOrder(params: Map<String, String>): Observable<BaseBean<Any?>> {
        return mHttpDataSource.evaluateOrder(params)
    }

    override fun evaluateComplaint(params: Map<String, String>): Observable<BaseBean<Any?>> {
        return mHttpDataSource.evaluateComplaint(params)
    }

    override fun evaluateHouseKeeper(params: Map<String, String>): Observable<BaseBean<Any?>> {
        return mHttpDataSource.evaluateHouseKeeper(params)
    }

    override fun getMonthPrice(vehicleNo: String, areaId: String): Observable<BaseBean<Float>> {
        return mHttpDataSource.getMonthPrice(vehicleNo, areaId)
    }

    override fun parkCharging(vehicleNo: String): Observable<BaseBean<PayDetail>> {
        return mHttpDataSource.parkCharging(vehicleNo)
    }

    override fun retrievePassword(
        phone: String,
        code: String,
        pwd: String
    ): Observable<BaseBean<Any?>> {
        return mHttpDataSource.retrievePassword(phone, code, pwd)
    }

    override fun getUserInfoNet(): Observable<BaseBean<UserInfo>> {
        return mHttpDataSource.getUserInfoNet()
    }

    override fun getLocalUserInfo(): UserInfo? {
        return mLocalDataSource.getLocalUserInfo()
    }

    override fun uploadHeadImg(imgSrc: String): Observable<BaseBean<ImgRspBean>> {
        return mHttpDataSource.uploadHeadImg(imgSrc)
    }

    override fun uploadHeadImg(
        bucketName: String,
        imgSrc: String,
        folderName: String
    ): Observable<BaseBean<ImgRspBean>> {
        return mHttpDataSource.uploadHeadImg(bucketName, imgSrc, folderName)
    }

    override fun changeUserInfo(params: Map<String, String>): Observable<BaseBean<Any?>> {
        return mHttpDataSource.changeUserInfo(params)
    }

    override fun uploadApplyFace(params: Map<String, String>): Observable<BaseBean<Any?>> {
        return mHttpDataSource.uploadApplyFace(params)
    }

    override fun queryHouseKeeperList(params: Map<String, String>): Observable<BaseBean<HouseKeeperBean>> {
        return mHttpDataSource.queryHouseKeeperList(params)
    }

    override fun getHouseKeepers(params: Map<String, String>): Observable<BaseBean<HouseKeeperListBean>> {
        return mHttpDataSource.getHouseKeepers(params)
    }

    override fun webGetHouseKeepers(params: Map<String, String>): Observable<BaseBean<HouseKeeperListBean>> {
        return mHttpDataSource.webGetHouseKeepers(params)
    }

    override fun getOrderType(pType: Int,areaNo: String): Observable<BaseBean<List<OrderType>>> {
        return mHttpDataSource.getOrderType(pType,areaNo)
    }

    override fun submitReport(sub: SubmitReportRequestBean): Observable<BaseBean<Any>> {
        return mHttpDataSource.submitReport(sub)
    }

    override fun submitComplaint(com: ComplaintSubmitRequestBean): Observable<BaseBean<Any?>> {
        return mHttpDataSource.submitComplaint(com)
    }

    override fun getOrderDetail(no: String): Observable<BaseBean<OrderDetailBean>> {
        return mHttpDataSource.getOrderDetail(no)
    }

    // 社区生活-疫情公告
    override fun getNoticeList(
        pageNo: Int,
        pageSize: Int,
        releaseType: Int,
        neighId: String
    ): Observable<BaseBean<NoticeListBean>> {
        return mTengyunDateSource.getNoticeList(pageNo, pageSize, releaseType, neighId)
    }

    override fun updaReadingVolumeById(
        announcementId: Int
    ): Observable<BaseBean<NoticeListBean>> {
        return mTengyunDateSource.updaReadingVolumeById(announcementId)
    }

    override fun selectAnnouncementById(
        announcementId: Int
    ): Observable<BaseBean<NoticeListBean.Data>> {
        return mTengyunDateSource.selectAnnouncementById(announcementId)
    }

    override fun selectHealthyAll(
        pageNo: Int,
        pageSize: Int,
        userId: Int,
        neighId: Int,
    ): Observable<BaseBean<HealthReportBean>> {
        return mTengyunDateSource.selectHealthyAll(pageNo, pageSize, userId, neighId)
    }

    override fun getAreaByUser(
        id: Int
    ): Observable<BaseBean<HealthUserBean.Data>> {
        return mTengyunDateSource.getAreaByUser(id)
    }

    override fun selectHealthyAndRelativesById(
        healthyId: Int
    ): Observable<BaseBean<HealthReportBean.Data>> {
        return mTengyunDateSource.selectHealthyAndRelativesById(healthyId)
    }

    override fun addHealthReport(params: HealthReportBean.Data): Observable<BaseBean<Any?>> {
        return mTengyunDateSource.addHealthReport(params)
    }

    /**
     * 查询缴费账单
     * type: 缴费类型
     */
    override fun getBillByRoomIdList(
        tUserId: Int,
        payStatus: Int,
        roomIdList : List<String>
    ): Observable<BaseBean<HousesPaymentBean>> {
        return mTengyunDateSource.getBillByRoomIdList(tUserId, payStatus, roomIdList)
    }

    /**
     * 查询缴费账单详情
     * type: 缴费类型
     */
    override fun getDetailsByBillId(
        billId: Int
    ): Observable<BaseBean<PaymentData>> {
        return mTengyunDateSource.getDetailsByBillId(billId)
    }

    /**
     * 插花地支付
     * type: 缴费类型
     */
    override fun payChdBill(
        paramsJson: String
    ): Observable<BaseBean<HouseOrderPayBean>> {
        return mTengyunDateSource.payChdBill(paramsJson)
    }

    override fun getComplaintDetail(no: String): Observable<BaseBean<ComplaintDetailBean>> {
        return mHttpDataSource.getComplaintDetail(no)
    }

    override fun getApplyDetails(applyId: String): Observable<BaseBean<ApplyDetailBean>> {
        return mHttpDataSource.getApplyDetails(applyId)
    }

    override fun applyAudit(
        applyId: String,
        approveRemark: String,
        approveStatus: String
    ): Observable<BaseBean<String>> {
        return mHttpDataSource.applyAudit(applyId, approveRemark, approveStatus)
    }

    override fun getApplyList(
        pageNum: Int,
        pageSize: Int,
        filter: String
    ): Observable<BaseBean<ApplyBean>> {
        return mHttpDataSource.getApplyList(pageNum, pageSize, filter)
    }

    override fun getApplyResult(
        applyId: String,
        mobile: String
    ): Observable<BaseBean<ApplyResultBean>> {
        return mHttpDataSource.getApplyResult(applyId, mobile)
    }

    override fun getTakeCareFullOrder(orderId: String): Observable<BaseBean<WorkOrderDetailBean>> {
        return mHttpDataSource.getTakeCareFullOrder(orderId)
    }

    override fun listFloor(
        buildingId: String
    ): Observable<BaseBean<List<FloorBean>>> {
        return mHttpDataSource.listFloor(buildingId)
    }

    override fun listBuilding(
    ): Observable<BaseBean<List<BuildingtBean>>> {
        return mHttpDataSource.listBuilding()
    }

    override fun listPointType(
        systemId: String
    ): Observable<BaseBean<List<DeviceTypeBean>>> {
        return mHttpDataSource.listPointType(systemId)
    }

    override fun takeCareOrderRecord(
        pageNum: Int,
        pageSize: Int,
        filter: TakeCareRecordFilterParams
    ): Observable<BaseBean<TakeCareRecordBean>> {
        return mHttpDataSource.takeCareOrderRecord(pageNum, pageSize, filter)
    }

    override fun addApply(params: GuestApplyParam): Observable<BaseBean<String>> {
        return mHttpDataSource.addApply(params)
    }

    override fun uploadImg(imgSrc: String): Observable<BaseBean<ImgRspBean>> {
        return mHttpDataSource.uploadImg(imgSrc)
    }

    override fun getCommunities(communityRequestBean: CommunityRequestBean): Observable<BaseBean<CommunityResponseBean>> {
        return mHttpDataSource.getCommunities(communityRequestBean)
    }

    override fun getNotices(communityRequestBean: CommunityRequestBean): Observable<BaseBean<NoticeResponseBean>> {
        return mHttpDataSource.getNotices(communityRequestBean)
    }

    override fun getActivityProfileList(params: Map<String, String>): Observable<BaseBean<ActivityProfileListBean>> {
        return mHttpDataSource.getActivityProfileList(params)
    }

    override fun getActivityDetail(activityId: String): Observable<BaseBean<ActivityDetailBean>> {
        return mHttpDataSource.getActivityDetail(activityId)
    }

    override fun joinActivity(activityId: String): Observable<BaseBean<Any?>> {
        return mHttpDataSource.joinActivity(activityId)
    }

    override fun takeCareOrderScanRecord(
        pageNum: Int,
        pageSize: Int,
        filter: String
    ): Observable<BaseBean<TakeCareRecordBean>> {
        return mHttpDataSource.takeCareOrderScanRecord(pageNum, pageSize, filter)
    }

    override fun getBulletinDetail(id: Int): Observable<BaseBean<BulletinDetailBean>> {
        return mHttpDataSource.getBulletinDetail(id)
    }
}